<?php
/**
 * 基于角色的权限控制管理
 * @author Administrator
 *
 */
 class RbacAction extends  CommonAction{
 	/**
 	 * 用户管理页面
 	 */
 	public function userMg() {
 		$this->userlist=D('UserRelation')->field('password',true)->relation(true)->select();
 		$this->display ( "userMg" );
 	}
 	/**
 	 * 角色管理页面
 	 */
 	public function roleMg() {
 		$arr = M ( 'role' )->select ();
 		$this->assign ( 'rolelist', $arr );
 		$this->display ( "roleMg" );
 	}
 	 /**
 	  * 节点管理页面
 	  */
 	public function nodeMg() {
 		$arr = M ( 'node' )->select ();
 		$this->assign ( 'nodelist', node_merge($arr) );
 		$this->display ( "nodeMg" );
 	}
 	/**
 	 * 添加用户页面
 	 */
 	public function userAdd() {
 		$field=array('id','name','remark');
 		$arr=M('role')->field($field)->select();
 		$this->assign('roles',$arr);
 		$this->display('userAdd');
 	}
 	/**
 	 * 添加角色页面
 	 */
 	public function roleAdd() {
 		$this->display ( "roleAdd" );
 	}
 	/**
 	 * 添加节点页面
 	 */
 	public function nodeAdd() {
 		$this->pid = I ( "pid", 0, 'intval' );
 		$this->level = I ( "level", 1, "intval" );
 		switch ($this->level) {
 			case 1 :
 				$this->type = "应用";
 				break;
 			case 2 :
 				$this->type = "控制器";
 				break;
 			case 3 :
 				$this->type = "动作方法";
 				break;
 			default :
 				$this->type = "其他";
 				break;
 		}
 	
 		$this->display ( "nodeAdd" );
 	}
 	
 	
 	/**
 	 * 添加用户页面
 	 */
 	public function addUser(){
 		$arr=array();
 		$arr['username']=I('username');
 		$arr['password']=I('password','','md5');
 		$arr['logintime']=time();
 		$arr['loginip']=get_client_ip();
 		$role=array();
 		if ($uid=M('user')->add($arr)) {
 			 foreach ($_POST['role_id'] as $v){
 			 	$role[]=array(
 			 		'role_id'=>$v,
 			 		'user_id'=>$uid
 			 	);
 			 }
 			 if(M('role_user')->addAll($role)){
 			 	$this->success('添加用户成功',U('Admin/Rbac/userMg'));
 			 }
 		}else{
 			$this->success("用户添加失败");
 		}
 		
 	}
 	/**
 	 * 添加角色页面
 	 */
 	public function addRole(){
 		$arr= array();
 		$arr['name']=I("name"); 
 		$arr['remark']=I("remark");
 		$arr['status']=I("status");
 		if (M("role")->add($arr)) {
 			$this->success('添加成功',U('Admin/Rbac/roleMg'));
 		}
 	}
 	/**
 	 * 添加节点
 	 */
 	public function addNode(){
 		
 		$arr=array();
 		$arr['name']=I("name");
 		$arr['title']=I("title");
 		$arr['status']=I("status");
 		$arr['sort']=I("sort");
 		$arr['pid']=I("pid");
 		$arr['level']=I("level");
 	    if (M("node")->add($arr)) {
 			$this->success('添加成功',U('Admin/Rbac/nodeMg'));
 		}
 		
 	}
 	/**
 	 * 权限配置页面
 	 */
 	public function access(){
 		$this->rid=I("rid");
 		//获取指定的字段用于显示
 		$field=array('id','name','title','pid');
 		$arr = M ( 'node' )->field($field)->select ();
 		
 		
 		
 		//获取已有的权限
 		$access=M('access')->where(array('role_id'=>$this->rid))->getField('node_id',true);
 		 
 		$this->assign ( 'nodelist', node_merge($arr,$access) );
 		$this->display();
 	}
 	
 	/**
 	 * 权限配置处理方法
 	 */
 	public function setAccess(){
 		 $rid=I("rid",0,'intval');
 		 $db=M('access');
 		 $db->where(array('role_id'=>$rid))->delete();
 		 
 		 
 		 $data=array();
 		 foreach ($_POST['access'] as $v){
 		 	$tmp=explode('_', $v);
 		 	$data[]=array('role_id'=>$rid,'node_id'=>$tmp[0],'level'=>$tmp[1]);
 		 	
 		 }
 		 if ($db->addAll($data)) {
 		 	$this->success('修改成功',U('Admin/Rbac/roleMg'));
 		 }else{
 		 	$this->error('修改失败');
 		 }
 	}
 	
 }

?>